package cn.wolfcode.cmis.servlet;

import cn.wolfcode.cmis.dao.IEmployeeDAO;
import cn.wolfcode.cmis.dao.lmpl.EmployeeDAOImpl;
import cn.wolfcode.cmis.domain.Employee;
import cn.wolfcode.cmis.qo.EmployeeQueryObject;
import cn.wolfcode.cmis.qo.PageResult;
import cn.wolfcode.cmis.service.IEmployeeService;
import cn.wolfcode.cmis.service.Impl.EmployeeServiceImpl;
import cn.wolfcode.cmis.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/lol/employee")
public class EmployeeServlet extends HttpServlet {
    IEmployeeDAO employeeDAO = new EmployeeDAOImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Object user = req.getSession().getAttribute("USER_IN_SESSION");
        if (user == null) {
        req.setAttribute("msg","请先登录再访问");
        req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
        //获取cmd的值再分发出去
        String cmd = req.getParameter("cmd");
        cmd = cmd == null ? "" : cmd;
        switch (cmd) {
            case "delete":
                delete(req, resp);
                break;
            case "input":
                input(req, resp);
                break;
            case "saveOrUpdate":
                saveOrUpdate(req, resp);
                break;
            default:
                list(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IEmployeeService employeeService = new EmployeeServiceImpl();
        EmployeeQueryObject qo = new EmployeeQueryObject();
        String username = req.getParameter("username");
        String minAge = req.getParameter("minAge");
        String maxAge = req.getParameter("maxAge");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        if (StringUtil.hasLength(username)) {
            qo.setUsername(username);

        }
        if (StringUtil.hasLength(minAge)) {
            qo.setMinAge(Integer.valueOf(minAge));

        }
        if (StringUtil.hasLength(maxAge)) {
            qo.setMaxAge(Integer.valueOf(maxAge));

        }
        if (StringUtil.hasLength(currentPage)) {
            qo.setCurrentPage(Integer.valueOf(currentPage));

        }
        if (StringUtil.hasLength(pageSize)) {
            qo.setPageSize(Integer.valueOf(pageSize));
        }
        PageResult<Employee> result = employeeService.query(qo);
        req.setAttribute("qo", qo);
        req.setAttribute("result", result);
        req.getRequestDispatcher("/list.jsp").forward(req, resp);
    }

    private void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Employee emp = new Employee();
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String age = req.getParameter("age");
        emp.setPassword(password);
        if (StringUtil.hasLength(username)) {
            emp.setUsername(username);
        }
        if (StringUtil.hasLength(age)) {
            emp.setAge(Integer.valueOf(age));
        }
        if (StringUtil.hasLength(id)) {
            emp.setId(Long.valueOf(id));
            employeeDAO.update(emp);
        } else {
            employeeDAO.insert(emp);
        }
        resp.sendRedirect("/lol/employee");
    }
    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (StringUtil.hasLength(id)) {
            Employee emp = employeeDAO.selectOne(Long.valueOf(id));
            req.setAttribute("emp", emp);
        }
        req.getRequestDispatcher("/input.jsp").forward(req, resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        if (StringUtil.hasLength(id)) {
            employeeDAO.delete(Long.valueOf(id));
        }
        resp.sendRedirect("/lol/employee");
    }
}
